package com.pearadmin.pro.modules.experiment.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.aop.annotation.Repeat;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfo;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfoCount;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfoRequest;
import com.pearadmin.pro.modules.experiment.entity.ExperimentMajor;
import com.pearadmin.pro.modules.experiment.service.ExperimentMajorService;
import com.pearadmin.pro.modules.experiment.service.ExperimentService;
import com.pearadmin.pro.modules.laboratoryManagement.domain.ApplicationRequest;
import com.pearadmin.pro.modules.laboratoryManagement.domain.Applicationtable;
import com.pearadmin.pro.modules.sys.domain.SysConfig;
import com.pearadmin.pro.modules.sys.param.SysConfigRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 实验空间门户
 *
 * @author ZhouXingYun
 * @date 2022/6/14 16:50
 */
@Api(tags = {"实验空间门户"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "eic")
public class ExperimentController extends BaseController {

    @Resource
    private ExperimentService experimentService;
    @Resource
    private ExperimentMajorService experimentMajorService;

    @Resource
    private UserContext userContext;


    /**
     * 首页推荐
     *
     * @return {@link Result}
     */
    @GetMapping("homeRecommendation")
    @Log(title = "首页推荐")
    @ApiOperation(value = "首页推荐")
    public Result homeRecommendation() {
        List<ExperimentInfo> experimentInfos = experimentService.homeRecommendation();
        Map<String, Object> returnMap = new HashMap<>();
        List<Map<String, String>> marqueeMap = new ArrayList<>();
        experimentInfos.forEach(experimentInfo -> {
            Map<String, String> map = new HashMap<>();
            String showLargerVersion = experimentInfo.getShowLargerVersion();
            experimentInfo.setImage(showLargerVersion);
            map.put("image",showLargerVersion);
            map.put("image",showLargerVersion);
            marqueeMap.add(map);
        });
        return success(experimentInfos);
    }


    /**
     * 条件筛选仿真实验
     *
     * @return {@link Result}
     */
    @GetMapping("screeningSimulationExperiments")
    @Log(title = "条件筛选仿真实验")
    @ApiOperation(value = "条件筛选仿真实验")
    public Result screeningSimulationExperiments(@RequestParam Map<String , String> param) {
       List<ExperimentInfo>  experimentInfoList = experimentService.screeningSimulationExperiments(param);
        return success(experimentInfoList);
    }
    /**
     * 单个实验详情
     *
     * @return {@link Result}
     */
    @GetMapping("detailsOfIndividualExperiments")
    @Log(title = "单个实验详情")
    @ApiOperation(value = "单个实验详情")
    public Result detailsOfIndividualExperiments(@RequestParam("id") String id) {
        ExperimentInfo  experimentInfoList = experimentService.detailsOfIndividualExperiments(id);
        return success(experimentInfoList);
    }


    /**
     * 项目管控列表
     * @author ZhouXingYun
     * @date 2022/6/21 9:55
     * @param request
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @GetMapping("page")
    @Log(title = "项目管控列表")
    @ApiOperation(value = "项目管控列表")
    public Result page(ExperimentInfoRequest request){
        return success(experimentService.page(request));
    }
    @Repeat
    @PostMapping("save")
    @Log(title = "新增虚仿")
    @ApiOperation(value = "新增虚仿")
    public Result save(@RequestBody @Validated ExperimentInfo experimentInfo) {
        Integer majorStatus = experimentInfo.getMajorStatus();
        ExperimentMajor byId = experimentMajorService.getById(majorStatus);
        experimentInfo.setMajor(byId.getParentId());
        experimentInfo.setCreateTime(new Date());
        experimentInfo.setCreateName(userContext.getUserId());
        return auto(experimentService.save(experimentInfo));
    }

    /**
     * 修改虚仿项目基本信息
     * @author ZhouXingYun
     * @date 2022/6/22 9:09
     * @param experimentInfo
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @PutMapping("edit")
    @Log(title = "修改虚仿项目基本信息")
    @ApiOperation(value = "修改虚仿项目基本信息")
    public Result edit(@RequestBody ExperimentInfo experimentInfo) {
        Integer majorStatus = experimentInfo.getMajorStatus();
        ExperimentMajor byId = experimentMajorService.getById(majorStatus);
        experimentInfo.setMajor(byId.getParentId());
        experimentInfo.setUpdateTime(new Date());
        experimentInfo.setUpdateName(userContext.getUserId());
        return auto(experimentService.updateById(experimentInfo));
    }

 /**
  * 删除虚仿项目
  * @author ZhouXingYun
  * @date 2022/6/22 10:21
  * @param id
  * @return com.pearadmin.pro.common.web.domain.Result
  */
    @DeleteMapping("remove")
    @Log(title = "删除虚仿项目")
    @ApiOperation(value = "删除虚仿项目")
    public Result remove(@RequestParam String id){
        return auto(experimentService.removeById(id));
    }

   /**
    * 批量删除虚仿项目
    * @author ZhouXingYun
    * @date 2022/6/22 10:21
    * @param ids
    * @return com.pearadmin.pro.common.web.domain.Result
    */
    @DeleteMapping("removeBatch")
    @Log(title = "批量删除虚仿项目")
    @ApiOperation(value = "批量删除虚仿项目")
    public Result removeBatch(@RequestParam List<String> ids) { return auto(experimentService.removeByIds(ids)); }

    // 统计
    @PutMapping("editCount")
    @ApiOperation(value = "修改实验次数")
    public Result editCount(@RequestBody ExperimentInfo experimentInfo) {
        return auto(experimentService.updateById(experimentInfo));
    }

}
